<script lang="ts" setup>
import {useRoute} from 'vue-router'
import {ref} from "vue"
import {getByIdArticle, postArticle, putArticle} from "../serve"
import JsImages from "./js-images.vue"
import {ElMessage} from "element-plus"
import JsEditor from "./js-editor.vue"
import {FileToBase64} from "../utils.ts"

const
    route = useRoute(),
    fromData = ref({
      id: '',
      "title": "",
      "imageInput": [],
      "synopsis": "",
      "status": true,
      "content": ''
    }),
    loading = ref(false),
    isAdd = ref(false),
    fromRules = {
      title: [
        {
          required: true,
          min: 1,
          max: 50,
          message: '文章标题长度1-50位',
          trigger: 'trigger',
        },
      ],
      imageInput: [
        {required: true, type: "array", min: 1, max: 6, message: "请上传1-6张图片", trigger: ["blur", "change"]}
      ],
      synopsis: [
        {
          required: true,
          min: 1,
          max: 200,
          message: '文章简介长度1-200位',
          trigger: 'trigger',
        },
      ],
      status: [
        {
          required: true,
          trigger: 'trigger',
        },
      ],
      content: [
        {
          required: true,
          min: 1,
          message: '请输入文章内容',
          trigger: 'trigger',
        },
      ]
    },
    froms = ref(),
    inImage = ref(false)
;

function get() {
  if (route.query['id']) {
    loading.value = true
    isAdd.value = false
    getByIdArticle(route.query['id']).then((r: any) => {
      console.log(r)
      r['imageInput'] = JSON.parse(r['imageInput'])
      fromData.value = r
      loading.value = false
      inImage.value = true
    }).catch(() => loading.value = false)
  } else {
    isAdd.value = true
  }
}

get()

function loadFile(e: any) {
  loading.value = true
  FileToBase64(e).then((r: any) => {
    fromData.value.imageInput = [r]
    loading.value = false
  })
}

async function change() {
  try {
    await froms.value.validate()
    if (isAdd.value) {
      postArticle({...fromData.value}).then(() => {
        ElMessage({type: 'success', message: '新增成功！'})
      }).catch(() => ElMessage({type: 'error', message: '新增失败！'}))
      return
    }
    putArticle({...fromData.value}).then(() => {
      ElMessage({type: 'success', message: '修改成功！'})
      get()
    }).catch(() => ElMessage({type: 'error', message: '修改失败！'}))
  } catch (e) {
  }
}

function deleteChange() {
  fromData.value.imageInput[0] = ''
}
</script>

<template>
  <el-scrollbar>
    <el-card v-loading="loading" box element-loading-text="加载中...">
      <template #header>{{ isAdd ? '鲜花资讯添加' : '鲜花资讯编辑' }}</template>
      <el-form ref="froms" :model="fromData" :rules="fromRules" el-From>
        <el-form-item label="文章标题：" prop="title">
          <el-input v-model="fromData['title']" maxlength="50" placeholder="请输入文章标题"/>
        </el-form-item>
        <el-form-item label="文章图片：" prop="imageInput">
          <jsImages v-if="isAdd?true:inImage" :picUrl="fromData.imageInput[0]" @myPicFile="loadFile"
                    @delete-change="deleteChange"/>
        </el-form-item>
        <el-form-item label="文章简介：" prop="synopsis">
          <el-input v-model="fromData['synopsis']" maxlength="200" placeholder="请输入文章介绍" rows="3"
                    type="textarea"/>
        </el-form-item>
        <el-form-item label="是否显示：" prop="status">
          <el-switch v-model="fromData['status']" active-text="是" inactive-text="否" inline-prompt/>
        </el-form-item>
        <el-form-item label="文章内容：" prop="content">
          <js-editor v-model="fromData.content" :Value="fromData.content" :default-config="{placeholder:'请输入文章内容...'}" :open="inImage" Height="400px"
                     style="width: 100%;"/>
        </el-form-item>
        <el-form-item>
          <el-button round style="width: 80%;margin: auto;" type="success" @click="change">立即提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </el-scrollbar>
</template>

<style lang="scss" scoped>
[box] {
  width: 80%;
  margin: auto;
}

[el-From] {
  width: 98%;
  margin: auto;
}
</style>